package com.tensquare.recruit.controller;

import com.tensquare.recruit.pojo.Recruit;
import com.tensquare.recruit.service.RecruitService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/23 17:04
 */
@RestController
@CrossOrigin
@RequestMapping("/recruit")
public class RecruitController {

    @Autowired
    private RecruitService recruitService;

    /**
     * fetch data by rule id
     *
     * @description  增加招聘
     * @param
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Recruit recruit){
        recruitService.add(recruit);
        return new Result(true, StatusCode.OK,"增加成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  招聘全部列表
     * @param
     * @return
     */
    @GetMapping
    public Result findAll(){
        return new Result(true,StatusCode.OK,"查询成功",recruitService.findAll());
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID查询招聘
     * @param
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id){
        return new Result(true,StatusCode.OK,"查询成功",recruitService.findById(id));
    }

    /**
     * fetch data by rule id
     *
     * @description  修改招聘
     * @param
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@RequestBody Recruit recruit,@PathVariable String id){
        recruit.setId(id);
        recruitService.update(recruit);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID删除招聘
     * @param
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id){
        recruitService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * fetch data by rule id
     *
     * @description  根据条件查询招聘列表
     * @param
     * @return
     */
    @PostMapping("/search")
    public Result findSearch(@RequestBody Map searchMap){
        return new Result(true,StatusCode.OK,"查询成功",recruitService.findSearch(searchMap));
    }

    /**
     * fetch data by rule id
     *
     * @description  招聘分页
     * @param
     * @return
     */
    @PostMapping("/search/{page}/{size}")
    public Result findSearch(@PathVariable int page,@PathVariable int size,@RequestBody Map searchMap){
        Page<Recruit> pageList = recruitService.findSearch(searchMap, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<>(pageList.getTotalElements(),pageList.getContent()));
    }

    /**
     * fetch data by rule id
     *
     * @description  推荐职位
     * @param
     * @return
     */
    @GetMapping("/search/recommend")
    public Result recommend(){
        List<Recruit> list = recruitService.findTop4ByStateOrderByCreatetimeDesc("2");
        return new Result(true,StatusCode.OK,"查询成功",list);
    }

    /**
     * fetch data by rule id
     *
     * @description  最新职位
     * @param
     * @return
     */
    @GetMapping("/search/newlist")
    public Result newList(){
        return new Result(true,StatusCode.OK,"查询成功",recruitService.newList());
    }
}
